package Utils;

import common.ServerInterface;
import io.restassured.response.Response;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 80736 on 2017/7/2.
 */
public class AccessToken {
    private static final String HOST = "http://helmet.yfsmart.com";
    private static final String URL = "/band/sso/user/bytoken/find.do";
    private static Logger logger = MyLogs.getLogger();

    /**
     * 根据header获取token值
     *
     * @param header
     * @return
     */
    public static String getToken(String header) {
        String[] r = header.split(",", 3);
        if (r.length > 1) {
            for (int i = 0; i < r.length; i++) {
                if (r[i].contains("accessToken")) {
                    return r[i].substring(12);
                }
            }
        }

        return null;
    }

    public static void main(String[] args) {
        String token = getToken("accessToken:LTAH1F19TVMKNFU2AOGXNJJF2P0A3M2J,Content-Type:application/json");
        logger.info("token:" + token);

    }

    /**
     * 根据header检查是否包含accesstoken值
     *
     * @param header
     * @return
     */
    public static boolean checkHeaderTokenExists(String header) {
        if (header != null) {
            String[] r = header.split(",", 2);
            if (r.length > 1) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 根据token值检查是否登陆
     *
     * @param token
     * @return
     */
    public static boolean isLogined(String token) {
        if (token != null) {
            String header = "application/json," + token;
            logger.info("header:" + header);
            Response response = Request.post(HOST, URL, header, null);
            response.prettyPrint();
            if (response.getStatusCode() == 200) {
                JsonUtil util = new JsonUtil("message");
                String message = util.getJsonData(response);
                if (message.equals("Sign is Expired, please Log In again")) {
                    return false;
                } else if (message.equals("Successful")) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 替换表格内header列的token值为有效值，并把对象list返回
     * @return
     */
    public static List<ServerInterface> replaceToken(List<ServerInterface> sList) {
//        ExcelReader reader = new ExcelReader();
//        List<ServerInterface> sList = reader.getExcelData(Config.TESTCASE_FILEPATH);
        List<ServerInterface> rList = new ArrayList<>();
        ServerInterface server = sList.get(0);
        Response response = Request.post(server.getHost(),server.getUrl(),server.getHeader(),server.getBody());
        response.prettyPrint();
        JsonUtil util = new JsonUtil("result.accessToken");
        String token = util.getJsonData(response);
        logger.info("token:" + token);

        for(ServerInterface s:sList){
            String header = s.getHeader();
            String reHeader = "";
            //判断header是否包含token
            if (AccessToken.checkHeaderTokenExists(header)) {

                //分割header值
                String[] tmp = header.split(",",3);
                for(int i = 0;i<tmp.length;i++){
                    if(tmp[i].contains("access")){
                        tmp[i] = "accessToken:" + token;
                    }
                }

                for (int i = 0; i < tmp.length; i++) {
                    reHeader += tmp[i] +",";
                }

                String lastStr = reHeader.substring(reHeader.length()-1, reHeader.length());
                if(lastStr.equals(",")){
                    reHeader = reHeader.substring(0,reHeader.length()-1);
                    s.setHeader(reHeader);
                    rList.add(s);
                }
            }else {
                rList.add(s);
            }
        }
        logger.info("服務器接口類型长度为："+rList.size());
        for (ServerInterface sif :
                rList) {
            logger.info("开始遍历对象。。。");
            logger.info(sif.getDescription()+sif.getHeader());
        }

        return rList;
    }
}
